package com.easyui.common;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.easyui.DB.ConnectDB;
import com.easyui.DB.OperateDB;

public class UserLogin {
	
	/**
	 * 检查登陆时的账号，密码以及身份是否正确
	 * 返回int类型
	 * @param role
	 * @param userID
	 * @param password
	 * @return
	 */
	public int check(String role, String userID, String password) {
		String sql = "";
		List<Object> list = null;
		List<Map> resultList = null;
		// boolean loginSuccess = false;
		int result = 0;

		sql = "select * from user where role = ? and userID = ?";
		list = new ArrayList<Object>();
		list.add(role);
		list.add(userID);
		OperateDB.setParmList(list);

		resultList = OperateDB.excuteQuery(sql);
		if (resultList.size() > 0) {// 数据库中有该用户时
			Map map = resultList.get(0);
			String psw = (String) map.get("passwd");// 获得对应账号的密码
			
			try {
				EncryptionDecryption des = new EncryptionDecryption("tourhb");// 自定义密钥
	
				if (des.encrypt(password).equals(psw)) {// 密码匹配
					result = 1;// 登录成功
				} else {
					System.out.println("密码错误！ ");
					result = 2;// 密码错误
				}
			} catch (Exception e) {
				result = 2;// 密码错误
                e.printStackTrace();
            }
		} else {
			System.out.println("数据库中没有该用户！ ");
			result = 3;// 数据库中没有该用户！
		}

		return result;
		// return loginSuccess;

	}

	/**
	 * 获得对应老师或者学生的姓名
	 * @param role
	 * @param userID
	 * @return
	 */
	public String getName(String role, String userID) {
		List<Object> list = null;
		List<Map> resultList = null;
		String sql = "";
		String userName = "";

		if (role.equals("teacher")) {
			sql = "select * from teacher where tno = ?";
		} else if(role.equals("student")){
			sql = "select * from student where sno = ?";
		}
		
		
		list = new ArrayList<Object>();
		list.add(userID);
		OperateDB.setParmList(list);

		resultList = OperateDB.excuteQuery(sql);
		if (resultList.size() > 0) {
			Map map = resultList.get(0);
			if (role.equals("teacher")) {
				userName = (String) map.get("tname");// 获得名字
			} else {
				userName = (String) map.get("sname");
			}
		}
		return userName;
	}
	
	/**
	 * 检查用户的密码是否正确
	 * @return
	 */
	public boolean checkOldPsw(String role, String userID, String oldPsw) {
		boolean success = false;
		
		List<Object> list = null;
		List<Map> resultList = null;
		String sql = "";

		try {
			
			EncryptionDecryption des = new EncryptionDecryption("tourhb");// 自定义密钥
	        String enpsw = des.encrypt(oldPsw);
	        
			sql = "select * from user where role = ? and userID = ? and passwd = ?";
			
			list = new ArrayList<Object>();
			list.add(role);
			list.add(userID);
			list.add(enpsw);
			OperateDB.setParmList(list);
	
			resultList = OperateDB.excuteQuery(sql);
			if (resultList.size() == 1) {
				success = true;
			} else {
				success = false;
			}
		} catch(Exception e) {
			success = false;
			e.printStackTrace();
		}
		
		return success;
	}
	
	/**
	 * 检查修改密码时新密码和重复密码是否相同
	 * @param newPsw
	 * @param rePsw
	 * @return
	 */
	public boolean checkTheSame(String newPsw, String rePsw) {
		boolean same = false;

		if(newPsw != null && !newPsw.trim().equals("") && rePsw != null && !rePsw.trim().equals("")) {
			if(newPsw.trim().equals(rePsw.trim())) {
				same = true;
			} else {
				same = false;
			}
		} else {
			same = false;
		}
		
		return same;
	}
	
	/**
	 * 设置新密码
	 * @param role
	 * @param userID
	 * @param oldPsw
	 * @param newPsw
	 * @return
	 */
	public boolean setNewPsw(String role, String userID, String newPsw) {
		List<Object> plist = null;
		boolean success = false;
		
		try {
			EncryptionDecryption des = new EncryptionDecryption("tourhb");// 自定义密钥
	        String enpsw = des.encrypt(newPsw);
			
			String	sql = "update user set passwd = ? where role = ? and userID = ?";
	
			plist = new ArrayList<Object>();	
			plist.add(enpsw);
			plist.add(role);
			plist.add(userID);
			OperateDB.setParmList(plist);//初始化paramList
			
			success = OperateDB.update(sql);
		} catch(Exception e) {
			success = false;
			e.printStackTrace();
		}
			
		return success;
	}
	
	/**
	 * 向user表插入数据
	 * @param conn
	 * @param tno
	 * @param tname
	 * @param sex
	 * @return
	 */
	public boolean insertUser(Connection conn, String role, String userID) {
		List<Object> plist = null;
		boolean success = false;
		
		try {
			EncryptionDecryption des = new EncryptionDecryption("tourhb");// 自定义密钥
	        String enpsw = des.encrypt("123456");
	        
			String	sql = "insert into user values(?, ?, ?)";
	System.out.println(sql);
	
			plist = new ArrayList<Object>();	
			plist.add(role);
			plist.add(userID);
			plist.add(enpsw);
			OperateDB.setParmList(plist);//初始化paramList
			
			success = OperateDB.update(conn, sql);
		} catch(Exception e) {
			success = false;
			e.printStackTrace();
		}
			
		return success;
	}
	
	/**
	 * 重置用户的密码
	 * @param userRole
	 * @param id
	 * @return
	 */
	public boolean resetPsw(String userRole, String[] id) {
		List<Object> plist = null;
		boolean success = true;
		
		if (id.length < 1) {
			success = false;
		} else {
			Connection conn = ConnectDB.getConnection();
			boolean autoCommit = false;
			try {
				autoCommit = conn.getAutoCommit();
				conn.setAutoCommit(false);// 取消自动提交
				
				EncryptionDecryption des = new EncryptionDecryption("tourhb");// 自定义密钥
	            String enpsw = des.encrypt("123456");
				
				for (int i = 0; i < id.length; i++) {
					String sql = "update user set passwd = ? where role = ? and userID = ?"; 
					plist = new ArrayList<Object>();	
					plist.add(enpsw);
					plist.add(userRole);
					plist.add(id[i]);
					OperateDB.setParmList(plist);//初始化paramList
					
					if(!OperateDB.update(conn,sql)) {
						success = false;
						break; // 跳出循环
					} else {
						System.out.println("重置密码  " + id[i] + "success");
					}
				}

			} catch (SQLException e) {
				success = false;
				System.out.println("重置密码失败，详情如下：\n");
				e.printStackTrace();
			} catch (NumberFormatException e) {
				success = false;
				System.out.println("数据格式化异常，详情如下：\n");
				e.printStackTrace();
			} catch (Exception e) {
				success = false;
				e.printStackTrace();
			} finally {
				try {
					if (success) {
						conn.commit();//手动提交
					} else {
						conn.rollback();//回滚
					}
					conn.setAutoCommit(autoCommit);// 恢复现场
				} catch (SQLException e) {
					e.printStackTrace();
				}
				
				OperateDB.close(conn);
			}
		}

		return success;
	}
	
	/**
	 * 获得用户信息
	 * @param role
	 * @param userID
	 * @return
	 */
	public List<Map> getUserInfo(String role, String userID) {
		List<Object> plist = null;
		List<Map> resultList = null;
		String sql = "";

		if (role.equals("teacher")) {
			sql = "select * from teacher where tno = ?";
		} else if(role.equals("student")){
			sql = "select * from student where sno = ?";
		}
		
		plist = new ArrayList<Object>();
		plist.add(userID);
		OperateDB.setParmList(plist);

		resultList = OperateDB.excuteQuery(sql);
		
		return resultList;
	}
	
}